/*
+------------------------------------------------------------------------------+
+ Purpose: This dofile creates a linked teacherid to match baseline student    +
+ data to teacher surveys                                                      +
+ Data it uses: Baseline student and teacher data                              +
+ Output file: Student data with teacherid linked variable (teacherid)                                                        
+ Date created: Sept 5, 2019                                                   +
+------------------------------------------------------------------------------+
*/

/*Note: variables entr_teach_401_check, same_teacher & entr_teach_401_dummy
were created for GENERATING the linked teacherid ONLY!
*/
clear all
set more off

cd "[directory]/02_Cleaned_Data"

use "Teacher_Survey_cleaned.dta" //load in baseline teacher survey
replace school_code_107 = 605118 if school_code_107 == 506118

keep school_name_106 school_code_107 first_name_109 family_name_109
gen teacheridstr1=school_code_107
tostring teacheridstr1, replace
gen pref="1"
gen teacherid = teacheridstr1 + pref
destring teacherid, replace 
drop teacheridstr1 pref
rename school_code_107 school_code_106

save "[directory]\teacherid_link.dta", replace


//meging to the student .dta
clear all
use "[directory]\Student_Survey_cleaned.dta"


replace school_code_106 = 405102 if school_code_106 == 504102 & school == 95

merge m:1 school_code_106 using "[directory]\teacherid_link.dta"



egen entr_teach_401_check = concat(family_name_109  first_name_109),punct(" ")
gen entr_teach_401_dummy = entr_teach_401

tostring teacherid, replace
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010331"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010351"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010361"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010381"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010401"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011031"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011181"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011261"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011281"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011351"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3040101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3040311"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3040321"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3040541"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041021"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041041"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041051"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041061"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041071"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041091"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041131"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041161"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3041231"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3050101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3050311"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3050331"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3050341"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3050371"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051191"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051241"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051261"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051291"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051311"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051351"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051401"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3051431"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3501011"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4010141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4010161"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4010301"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4010321"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011191"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011221"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011231"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011261"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4020111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4020321"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4020521"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021031"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021091"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021161"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021211"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4030301"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4030311"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4030501"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4030571"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031011"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031041"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031081"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031131"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031211"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031221"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031231"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031241"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031281"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031291"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4031321"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4050121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4050331"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4050351"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051021"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051041"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051051"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051131"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051191"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051201"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051241"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051261"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051301"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4051331"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030301"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030311"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030501"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030521"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030531"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5030551"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031001"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031051"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031071"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031181"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031191"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031211"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031221"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5031231"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040181"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040191"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040201"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040541"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5040561"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041041"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041071"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041081"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041091"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5041241"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5060161"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5060321"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061001"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061011"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061021"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061051"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061071"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061091"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061131"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061141"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061151"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5061171"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5070101"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5070341"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5070541"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071001"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071011"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071021"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071071"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071181"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071201"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071211"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071231"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "35030011"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "50304031"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "405191361"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010371"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3010131"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3011291"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "3040331"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4010271"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4011121"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4021111"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "4030211"
replace entr_teach_401_dummy = entr_teach_401_check if teacherid == "5071151"

//checking a teacher's name appearing in both student and teacher .dta
//i.e for schools where students named more than one teacher
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:7be96f8b-310f-4137-aa30-8af5cf876ca8"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:d4fed69c-d214-4923-b3cd-370c18d6f58a"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:9823ec95-bc06-4eee-b748-278c395e4814"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:1d2ba348-4402-4b5d-ab3b-f04e09cc7f10"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:bace1e62-33e4-48f2-95f1-2898b00374ae"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:d58755e2-17ec-41db-96c1-666f04a57288"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:c4247bb4-dc67-44e0-981f-24296deee62d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:d03873c9-81f7-42c6-a274-ce6202d85aaa"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:c923b014-1f3b-4d04-8e29-c672b528256d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:ccce5952-d415-4a05-9b53-2045ecc0aa99"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:5e9e3a11-c6b1-419a-9115-d6d6778bc48d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:6d4e074d-f3c9-4e8e-aba1-558ab38a1432"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:f0b7528c-05f5-449f-bf6b-d4cc5ef37765"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:ba097f4d-8266-4ef1-8787-69950da301b7"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:d89746b9-25de-4cb9-85d5-36555f84f0d8"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:ef7cf565-0a3e-4a40-9bce-5f3ea0707511"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:3414740f-0472-4f92-8f78-e55e8c127fb9"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:2a9f220e-ffb9-4fe0-aa0f-e4e55609bc59"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:c22923d0-7f9b-47ba-b770-eefcdc84efe8"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:619a9466-a652-4aad-8b1f-e78a91227fa6"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:248c60eb-09ca-42e1-82a6-f025e3add331"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:c641da41-b73e-4309-9e3b-58166ac3c5b0"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:3ae5ad24-7060-47bf-ab7a-8c2cd2721d3f"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:e2c04b51-75d9-4a31-a688-8c13a02c7f9c"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:88898195-86a0-4da1-98c2-950cf2da6947"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:314d40b1-0766-4188-9f06-edce1f74ab30"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:5935dc51-948a-4a57-b7ec-afe914cdfe8d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:62c8e35b-5d56-4e3e-80a1-eeb7606cace3"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:e7c728ee-c8af-4981-b6af-01c8a7ba04b6"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:3d0371e2-c0a5-49f6-8ca5-8eaf7a04b59d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:ab4f04df-5f09-49c2-be9a-4ea2fcdbf752"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:62d234dc-51f7-4d3f-a314-8786150f4faf"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:28c95386-711b-4e64-aa36-fddfbb915e2b"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:0fdb5ac8-fada-491b-b362-99269acb19f2"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:53db8df3-f37d-4802-966d-854109a48c5a"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:e33d7322-2f97-48ce-8623-a4d6f8738651"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:e4323bd4-d215-4b76-beaf-8fc231c1c5b8"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:d50af89a-c502-4e23-b4ad-863fa1227654"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:64fbb117-4a24-43da-8eb2-0ef7a0ab2911"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:3e12d799-80b7-409e-882e-4727a4938224"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:b8828a8c-63a4-45c0-8c11-ef8c135b7240"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:931fa783-b04f-4a9f-9fe3-9f4e4317781d"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:446b010c-f40f-42b9-a839-0cd23c32861f"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:831d88a6-89b9-4edc-af64-b4e26140ff1b"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:7e4ed5f9-3e41-4cfb-8134-37bde0f92dd3"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:19864401-fe06-4860-81e6-df2714ff3219"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:cad83de9-3a50-431a-b23d-0cf53fd98d45"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:9d8c8de7-c134-4af0-b254-8af04798fa78"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:53680516-3e5e-4565-bc65-7f15d973d763"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:e727cacc-6087-486d-90e6-772d418e1824"
replace entr_teach_401_dummy = entr_teach_401_check if KEY == "uuid:388b11ad-6837-44b4-b62b-b4a23ec9bb06"


//creating dummy variables to check if it's the same teacher
gen same_teacher = "" 
replace same_teacher = "1" if entr_teach_401_dummy == entr_teach_401_check
replace same_teacher = "0" if same_teacher == ""
order entr_teach_401_dummy, after(entr_teach_401) 


destring same_teacher, replace
la var same_teacher "Is s/he the same teacher in both student and teacher data?"
la de same_teacher 1 "Yes" 0 "No"
la val same_teacher same_teacher




order school_code_106, after(school)
order school_name_106, after(school)
order teacherid first_name_109 family_name_109 entr_teach_401_check, after(entr_teach_401)
destring teacherid, replace
drop _merge

save "[directory]/Student_Survey_cleaned_linked_PIIs.dta", replace

clear all 
use "[directory]/Student_Survey_cleaned_linked_PIIs.dta"

//Encripting PIIs
do "[directory]\nopiis_student_survey_linked.do"

save "[directory]/Student_Survey_cleaned_linked_NOPIIs.dta", replace



